﻿using dll_DynamicDataDisplay.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace dll_DynamicDataDisplay.Views
{
    /// <summary>
    /// Interaction logic for View_Chart.xaml
    /// </summary>
    public partial class View_Chart : UserControl
    {
        /// <summary>
        /// 图表
        /// </summary>
        public Vm_Chart Chart
        {
            get { return (Vm_Chart)GetValue(ChartProperty); }
            set { SetValue(ChartProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Chart.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ChartProperty =
            DependencyProperty.Register("Chart", typeof(Vm_Chart), typeof(View_Chart), 
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnChartChanged)));


        /// <summary>
        /// 图例可见性
        /// </summary>
        public bool LegendVisible
        {
            get { return (bool)GetValue(LegendVisibleProperty); }
            set { SetValue(LegendVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LegendVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LegendVisibleProperty =
            DependencyProperty.Register("LegendVisible", typeof(bool), typeof(View_Chart), new PropertyMetadata(false));


        /// <summary>
        /// 标题可见性
        /// </summary>
        public bool HeaderVisible
        {
            get { return (bool)GetValue(HeaderVisibleProperty); }
            set { SetValue(HeaderVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderVisibleProperty =
            DependencyProperty.Register("HeaderVisible", typeof(bool), typeof(View_Chart), new PropertyMetadata(false));


        /// <summary>
        /// 横坐标标题可见性
        /// </summary>
        public bool HorTitleVisible
        {
            get { return (bool)GetValue(HorTitleVisibleProperty); }
            set { SetValue(HorTitleVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HorTitleVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HorTitleVisibleProperty =
            DependencyProperty.Register("HorTitleVisible", typeof(bool), typeof(View_Chart), new PropertyMetadata(false));


        /// <summary>
        /// 纵坐标标题可见性
        /// </summary>
        public bool VerTitleVisible
        {
            get { return (bool)GetValue(VerTitleVisibleProperty); }
            set { SetValue(VerTitleVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VerTitleVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VerTitleVisibleProperty =
            DependencyProperty.Register("VerTitleVisible", typeof(bool), typeof(View_Chart), new PropertyMetadata(false));


        /// <summary>
        /// 标题
        /// </summary>
        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(string), typeof(View_Chart), new PropertyMetadata("标题"));


        /// <summary>
        /// 横坐标标题
        /// </summary>
        public string HorTitle
        {
            get { return (string)GetValue(HorTitleProperty); }
            set { SetValue(HorTitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HorTitle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HorTitleProperty =
            DependencyProperty.Register("HorTitle", typeof(string), typeof(View_Chart), new PropertyMetadata("横坐标标题"));


        /// <summary>
        /// 纵坐标标题
        /// </summary>
        public string VerTitle
        {
            get { return (string)GetValue(VerTitleProperty); }
            set { SetValue(VerTitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VerTitle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VerTitleProperty =
            DependencyProperty.Register("VerTitle", typeof(string), typeof(View_Chart), new PropertyMetadata("纵坐标标题"));


        /// <summary>
        /// 图表类更改事件响应方法
        /// </summary>
        /// <param name="d"></param>
        /// <param name="args"></param>
        private static void OnChartChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            View_Chart uc = d as View_Chart;

            if (args.OldValue != null)
            {
                Vm_Chart oldChart = args.OldValue as Vm_Chart;
                oldChart.ElementCollection.CollectionChanged -= uc.ElementCollection_CollectionChanged;

                //移除旧项目
                foreach (var item in oldChart.ElementCollection)
                    uc.Remove(item);
            }

            if (args.NewValue != null)
            {
                Vm_Chart newChart = args.NewValue as Vm_Chart;
                newChart.ElementCollection.CollectionChanged += uc.ElementCollection_CollectionChanged;

                //添加新项目
                foreach (var item in newChart.ElementCollection)
                    uc.Add(item);

                //更新图例显示状态
                if (uc.LegendVisible)
                {
                    uc.plotter.LegendVisibility = Visibility.Visible;
                }
                else
                {
                    uc.plotter.LegendVisibility = Visibility.Collapsed;
                }

                //更新标题
                uc.header.Content = uc.Header;
                if (uc.HeaderVisible)
                {
                    uc.header.Visibility = Visibility.Visible;
                }
                else
                {
                    uc.header.Visibility = Visibility.Collapsed;
                }

                //更新横坐标标题
                uc.horTitle.Content = uc.HorTitle;
                if (uc.HorTitleVisible)
                {
                    uc.horTitle.Visibility = Visibility.Visible;
                }
                else
                {
                    uc.horTitle.Visibility = Visibility.Collapsed;
                }

                //更新纵坐标标题
                uc.verTitle.Content = uc.VerTitle;
                if (uc.VerTitleVisible)
                {
                    uc.verTitle.Visibility = Visibility.Visible;
                }
                else
                {
                    uc.verTitle.Visibility = Visibility.Collapsed;
                }
            }
        }

        /// <summary>
        /// 图表元素集合改变事件响应方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ElementCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            //移除旧元素
            if (e.OldItems != null)
                foreach (var item in e.OldItems)
                    this.Remove(item as Vm_Element);
            
            //添加新元素
            if (e.NewItems != null)
                foreach (var item in e.NewItems)
                    this.Add(item as Vm_Element);
        }

        /// <summary>
        /// ctor
        /// </summary>
        public View_Chart()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 压入新元素
        /// </summary>
        /// <param name="Ele"></param>
        void Add(Vm_Element Ele)
        {
            Ele.ChartPlotter_Attach(this.plotter);
        }

        /// <summary>
        /// 移除旧元素
        /// </summary>
        /// <param name="Ele"></param>
        void Remove(Vm_Element Ele)
        {
            Ele.ChartPlotter_Detach();
        }

    }
}
